package com.baoyi.pooke;

import java.io.File;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.baoyi.content.content;
import com.baoyi.utils.DiskImageCache;
import com.baoyi.utils.ImageCache;
import com.baoyi.utils.Utils;

public class BaoyiApplication extends Application {
	/**
	 * Singleton pattern
	 */
	private static BaoyiApplication instance;
	/**
	 * Tag used for DDMS logging
	 */
	public static String TAG = "baoyi";

	public static BaoyiApplication getInstance() {
		return instance;
	}

	private void inticacheDisk() {

		File catalog = new File(content.locdir);
		if (!(catalog.exists()) && !(catalog.isDirectory())) {
			boolean creadok = catalog.mkdirs();
			if (creadok) {
				System.out.println(" ok:创建文件夹成功！ ");
			} else {
				System.out.println(" err:创建文件夹失败！ ");
			}
		}
		File disk = new File(content.pookedir);
		if (!(disk.exists()) && !(disk.isDirectory())) {
			boolean creadok = disk.mkdirs();
			if (creadok) {
				System.out.println(" ok:创建文件夹成功！ ");
			} else {
				System.out.println(" err:创建文件夹失败！ ");
			}
		}
	}

	public String getLocalMacAddress() {
		WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		String mac = info.getMacAddress();
		if (null == mac) {
			mac = "";
		}
		return mac;
	}

	/**
	 * Image cache, one for all activities and orientations
	 */
	private ImageCache mImageCache;

	/**
	 * Access to global image cache across Activity instances
	 * 
	 * @return
	 */
	public ImageCache getImageCache() {
		return mImageCache;
	}

	/**
	 * Retrieves application's version number from the manifest
	 * 
	 * @return
	 */
	public String getVersion() {
		String version = "0.0.0";

		PackageManager packageManager = getPackageManager();
		try {
			PackageInfo packageInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			version = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return version;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		if (Utils.readSDCardMB() > 50) {
			mImageCache = new DiskImageCache();

		} else {
			mImageCache = new ImageCache();
		}
		inticacheDisk();
		Log.d(TAG, getLocalMacAddress());

	}

	public boolean isonline() {
		ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cManager.getActiveNetworkInfo();
		if (info != null && info.isAvailable()) {
			// do something
			// 能联网
			return true;
		} else {
			// 不能联网
			return false;
		}
	}
}
